import { asyncRoutes, constantRoutes } from '@/router'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      let accessedRoutes
      if (roles.includes('ROLE_ADMIN')) {
        accessedRoutes = asyncRoutes || []
      } else {
        accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      }
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

// import { constantRouterMap } from '@/router/routers'
// import Layout from '@/layout/index'
// import ParentView from '@/components/ParentView'

// const permission = {
//   state: {
//     routers: constantRouterMap,
//     addRouters: [],
//     sidebarRouters: []
//   },
//   mutations: {
//     SET_ROUTERS: (state, routers) => {
//       state.addRouters = routers
//       state.routers = constantRouterMap.concat(routers)
//     },
//     SET_SIDEBAR_ROUTERS: (state, routers) => {
//       state.sidebarRouters = routers
//     }
//   },
//   actions: {
//     GenerateRoutes({ commit }, asyncRouter) {
//       commit('SET_ROUTERS', asyncRouter)
//     },
//     SetSidebarRouters({ commit }, sidebarRouter) {
//       commit('SET_SIDEBAR_ROUTERS', sidebarRouter)
//     }
//   }
// }

// export const filterAsyncRouter = (routers, isRewrite = false) => { // 遍历后台传来的路由字符串，转换为组件对象
//   return routers.filter(router => {
//     if (isRewrite && router.children) {
//       router.children = filterChildren(router.children)
//     }
//     if (router.component) {
//       if (router.component === 'Layout') { // Layout组件特殊处理
//         router.component = Layout
//       } else if (router.component === 'ParentView') {
//         router.component = ParentView
//       } else {
//         const component = router.component
//         router.component = loadView(component)
//       }
//     }
//     if (router.children && router.children.length) {
//       router.children = filterAsyncRouter(router.children, router, isRewrite)
//     }
//     return true
//   })
// }

// function filterChildren(childrenMap) {
//   var children = []
//   childrenMap.forEach((el, index) => {
//     if (el.children && el.children.length) {
//       if (el.component === 'ParentView') {
//         el.children.forEach(c => {
//           c.path = el.path + '/' + c.path
//           if (c.children && c.children.length) {
//             children = children.concat(filterChildren(c.children, c))
//             return
//           }
//           children.push(c)
//         })
//         childrenMap.splice(index, 1)
//         return
//       }
//     }
//     children = children.concat(el)
//   })
//   return children
// }

// export const loadView = (view) => {
//   return (resolve) => require([`@/views/${view}`], resolve)
// }

// export default permission
